﻿if (typeof (Solution) == 'undefined') Solution = {};
if (typeof (Solution.Admin) == 'undefined') Solution.Admin = {};
Solution.Admin.jCommon = {
    Init: function () {
    },
    CreateNews: function (gridID) {
        var thisObj = this;
        $(gridID).editGridRow("new", {
            closeAfterEdit: true,
            closeAfterAdd: true,
            afterSubmit: function (response, postdata) {
                if (response.responseText != '"err"') {
                    Solution.Admin.Core.ShowNotif('s', 0);
                    thisObj.ReloadGrid(gridID);
                }
                else
                    Solution.Admin.Core.ShowNotif('e', -1);
                return true;
            }
        });
    },
    ReloadGrid: function (gridID) {
        //Solution.Core.WaitWithOverlay('wait.............');
        $(gridID).trigger("reloadGrid");
        // Solution.Core.RemoveWaitThenOverlay();
        Solution.Admin.Core.ShowNotif('s', 0);
    },
    ShowBin: function (gridID, Url, showBin) {
        if (typeof showBin != 'undefined') {
            if (showBin)
                Url += '?showBin=true';
            else
                Url += '?showBin=false';
        }
        $(gridID).jqGrid().setGridParam({ url: Url }).trigger("reloadGrid");
    },
    DeleteRows: function (gridID, _URL, delForever) {
        var thisObj = this;
        ids = $(gridID).jqGrid('getGridParam', 'selarrrow');
        if (typeof ids == 'undefined' || ids.length < 1) {
            jAlert('choose <b>at least 1 row</b>!', '~');
            return;
        }
        var _alert = 'do you want delete?';
        if (delForever)
            var _alert = 'delete forever <b>every data relation </b> will delete?';
        jConfirm(_alert, '~', function (r) {
            if (r) {

                var grid = $(gridID);
                var ids = grid.getDataIDs();
                ids = $(gridID).jqGrid('getGridParam', 'selarrrow');
                var postData = $.toJSON({ oper: 'del', id: ids });
                if (delForever)
                    var postData = $.toJSON({ oper: 'del', delForever: true, id: ids });
                $.ajax({
                    url: _URL,
                    data: postData,
                    type: 'POST',
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    success: function (result) {
                        if (result != 'err') {
                            thisObj.ReloadGrid(gridID);
                            Solution.Admin.Core.ShowNotif('s', 0);
                        }
                        else {
                            Solution.Admin.Core.ShowNotif('e');
                        }

                    },
                    error: function () {
                        Solution.Admin.Core.ShowNotif('e', -1, 'Ajax error');
                    }

                });

            }
        });
    },
    beforeSelectRow: function (gridID, rowid, e, lastselObj) {
        if (typeof lastselObj != 'undefined') {
            if (rowid != lastselObj.lastsel) {
                jQuery(gridID).jqGrid('restoreRow', lastselObj.lastsel);
                lastselObj.lastsel = 0;
                if (!e.ctrlKey && !e.shiftKey) {
                    $(gridID).jqGrid('resetSelection');
                }
                else if (e.shiftKey) {
                    var initialRowSelect = $(gridID).jqGrid('getGridParam', 'selrow');
                    $(gridID).jqGrid('resetSelection');
                    var CurrentSelectIndex = $(gridID).jqGrid('getInd', rowid);
                    var InitialSelectIndex = $(gridID).jqGrid('getInd', initialRowSelect);
                    var startID = "";
                    var endID = "";
                    if (CurrentSelectIndex > InitialSelectIndex) {
                        startID = initialRowSelect;
                        endID = rowid;
                    }
                    else {
                        startID = rowid;
                        endID = initialRowSelect;
                    }
                    var shouldSelectRow = false;
                    var DataIDs = $(gridID).getDataIDs();
                    for (var i = 0; i < DataIDs.length; i++) {
                        if ((shouldSelectRow = DataIDs[i] == startID || shouldSelectRow)) {
                            $(gridID).jqGrid('setSelection', DataIDs[i], true);
                        }
                        if (DataIDs[i] == endID) {
                            return false;
                        }
                    }
                }
            }
        }
        return true;
    }
}
